home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 445_01 / pi5ways / pi2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-05  |  1.6 KB  |  47 lines

  1. /**************************************************************************/
  2. /*       Calculation of π by James Gregory (ca. 1671) approximation       */
  3. /*     π = 4 { 1 - 1/3 + 1/5 - 1/7 + 1/9 ... +[(-1)**n]/[2*k+1] ... }     */
  4. /*                                                                        */
  5. /*                                                                        */
  6. /*                                M\Cooper                                */
  7. /*                       3425 Chestnut Ridge Rd.                          */
  8. /*                        Grantsville, MD 21536                           */
  9. /*                       -------------------------                        */
  10. /*                       email: thegrendel@aol.com                        */   
  11. /*                                                                        */
  12. /*                                   06/91                                */
  13. /*                   Source code placed in the public domain              */                 
  14. /**************************************************************************/
  15.  
  16. #include <stdio.h>
  17.  
  18. #define MAX 5000
  19.  
  20. void main()
  21. {
  22.    double intermediate_result = 0,
  23.           numerator,
  24.           Pi;
  25.    register int k;
  26.  
  27.  
  28.  
  29.  
  30.       for( k = 0; k <= MAX; k++ )
  31.         {  
  32.         if( k % 2 )
  33.            numerator = -1.0;
  34.         else numerator = 1.0;
  35.       
  36.         intermediate_result += numerator / ( 2 * (double)k + 1 );
  37.         Pi = 4.0 * intermediate_result;
  38.  
  39.         printf( "Term #%5d ------>  π ≈ %f  \n", k, Pi ); 
  40.         }
  41.  
  42. }
  43.  
  44.  
  45.  
  46.  
  47.